package com.cfuture08.eweb4j.orm.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.cfuture08.eweb4j.cache.ORMConfigBeanCache;
import com.cfuture08.eweb4j.cache.SingleBeanCache;
import com.cfuture08.eweb4j.config.ConfigConstant;
import com.cfuture08.eweb4j.config.bean.ConfigBean;
import com.cfuture08.eweb4j.config.log.LogFactory;
import com.cfuture08.eweb4j.jdbc.ConnectionManager;
import com.cfuture08.eweb4j.jdbc.JdbcTemplate;
import com.cfuture08.eweb4j.jdbc.sql.OrderType;
import com.cfuture08.eweb4j.jdbc.sql.SqlFactory;
import com.cfuture08.eweb4j.orm.cache.LevelOneCache;
import com.cfuture08.eweb4j.orm.cache.SqlResultCache;
import com.cfuture08.eweb4j.orm.config.bean.ORMConfigBean;
import com.cfuture08.util.LogUtil;

/**
 * 基本的公共DAO操作类
 * 
 * @author cfuture.aw
 * @since v1.a.0
 */
public class DAO {
	/**
	 * 将pojo数据插入到数据库 支持多个不同的pojo同时插入 是事务安全的
	 * 
	 * @param <T>
	 * @param ts
	 *            多个不同的保存有数据的pojo
	 * @return 返回布尔值
	 */
	public static <T> boolean insert(T... ts) {
		boolean result = false;
		if (ts != null && ts.length > 0) {
			Class<?> clazz = ts[0].getClass();
			try {
				result = JdbcTemplate.update(clazz, SqlFactory.getInsertSql(ts)
						.create());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	public static <T> boolean insertWhere(String condition, T... ts) {
		boolean result = false;
		if (ts != null && ts.length > 0) {
			Class<?> clazz = ts[0].getClass();
			try {
				result = JdbcTemplate.update(clazz, SqlFactory.getInsertSql(ts)
						.create(condition));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 删除记录(按主键)
	 * 
	 * @param <T>
	 * @param ts
	 * @return
	 */
	public static <T> boolean delete(T... ts) {
		boolean result = false;
		if (ts != null && ts.length > 0) {
			Class<?> clazz = ts[0].getClass();
			try {
				result = JdbcTemplate.update(clazz, SqlFactory.getDeleteSql(ts)
						.delete());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 删除记录，按给定字段
	 * 
	 * @param <T>
	 * @param t
	 * @param columns
	 * @return
	 */
	public static <T> boolean delete(T t, String[] columns) {
		boolean result = false;
		if (t != null) {
			try {
				result = JdbcTemplate.update(t.getClass(), SqlFactory
						.getDeleteSql(t).delete(columns));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 删除记录，按给定字段、给定值
	 * 
	 * @param <T>
	 * @param clazz
	 * @param columns
	 * @param values
	 * @return
	 */
	public static <T> boolean delete(Class<T> clazz, String[] columns,
			String values[]) {
		boolean result = false;
		if (clazz != null && columns != null && values != null
				&& columns.length == values.length) {
			try {
				result = JdbcTemplate.update(clazz, SqlFactory.getDeleteSql(
						clazz.newInstance()).delete(columns, values));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	public static <T> boolean deleteWhere(Class<T> clazz, String condition) {
		boolean result = false;
		if (clazz != null) {
			try {
				result = JdbcTemplate.update(clazz, SqlFactory.getDeleteSql(
						clazz.newInstance()).delete(condition));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 修改，按主键,可以更新多个不同表记录，且是事务安全的
	 * 
	 * @param <T>
	 * @param ts
	 *            给定的一个或多个对象
	 * @return 返回布尔
	 */
	public static <T> boolean update(T... ts) {
		boolean result = false;
		if (ts != null && ts.length > 0) {
			Class<?> clazz = ts[0].getClass();
			try {
				result = JdbcTemplate.update(clazz, SqlFactory.getUpdateSql(ts)
						.update());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	public static <T> boolean updateWhere(String condition, T... ts) {
		boolean result = false;
		if (ts != null && ts.length > 0) {
			Class<?> clazz = ts[0].getClass();
			try {
				result = JdbcTemplate.update(clazz, SqlFactory.getUpdateSql(ts)
						.update(condition));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 修改，对给定字段修改
	 * 
	 * @param <T>
	 * @param t
	 *            给定的对象
	 * @param columns
	 *            给定的字段
	 * @return 返回布尔
	 */
	public static <T> boolean update(T t, String[] columns) {
		boolean result = false;
		if (t != null && columns != null) {
			try {
				result = JdbcTemplate.update(t.getClass(), SqlFactory
						.getUpdateSql(t).update(columns));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 查询表中所有记录
	 * 
	 * @param <T>
	 * @param clazz
	 *            给定类型
	 * @param orderColumn
	 *            排序字段
	 * @param orderType
	 *            排序类型 -1降序 1升序
	 * @return
	 */
	public static <T> List<T> selectAll(Class<T> clazz, String orderColumn,
			int orderType) {
		List<T> list = null;
		if (clazz != null) {
			try {
				String sql = SqlFactory.getSelectSql(clazz.newInstance())
						.selectAll(orderColumn, orderType);
				if (LevelOneCache.containsKey(clazz)) {
					SqlResultCache src = LevelOneCache.get(clazz);
					if (src.containsKey(sql)) {
						list = (List<T>) LevelOneCache.get(clazz).get(sql);
					} else {
						list = JdbcTemplate.getResultSet(clazz, sql);
						src.add(sql, list);
					}
				} else {
					SqlResultCache src = new SqlResultCache();
					list = JdbcTemplate.getResultSet(clazz, sql);
					src.add(sql, list);
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	/**
	 * 查询所有，默认id排序
	 * 
	 * @param <T>
	 * @param clazz
	 * @param orderType
	 * @return
	 */
	public static <T> List<T> selectAll(Class<T> clazz, int orderType) {
		return DAO.selectAll(clazz, null, orderType);
	}

	/**
	 * 查询所有，默认id排序且降序
	 * 
	 * @param <T>
	 * @param clazz
	 * @return
	 */
	public static <T> List<T> selectAll(Class<T> clazz) {
		return DAO.selectAll(clazz, null, OrderType.DESC_ORDER);
	}

	/**
	 * 查找符合主键值的记录（支持复合主键）
	 * 
	 * @param <T>
	 * @param t
	 *            映射数据表的保存着主键值数据的pojo
	 * @return 返回一个充满表中所有字段值的pojo
	 */
	public static <T> T selectOne(T t) {
		T result = null;
		if (t != null) {
			Class<T> clazz = (Class<T>) t.getClass();
			try {
				String sql = SqlFactory.getSelectSql(t).selectOne();
				List<T> list = JdbcTemplate.getResultSet(clazz, sql);
				if (list != null && !list.isEmpty()) {
					result = list.get(0);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 查询单个记录，当给定条件只能查询单条记录时使用，按id排序且降序
	 * 
	 * @param <T>
	 * @param t
	 * @param columns
	 * @return
	 */
	public static <T> T selectOne(T t, String[] columns) {
		List<T> list = DAO.search(t, columns);
		return list == null ? null : list.get(0);
	}

	/**
	 * 查询单个记录，当确认给定条件是只能够查询一条记录时使用
	 * 
	 * @param <T>
	 * @param clazz
	 * @param columns
	 * @param values
	 * @param orderColumn
	 * @param orderType
	 * @return
	 */
	public static <T> T selectOne(Class<T> clazz, String[] columns,
			String[] values, String orderColumn, int orderType) {
		List<T> list = DAO.search(clazz, columns, values, orderColumn,
				orderType);
		return list == null ? null : list.get(0);
	}

	/**
	 * 查询单个记录，当确认给定条件只能查询一条记录时使用,默认id排序
	 * 
	 * @param <T>
	 * @param clazz
	 * @param columns
	 * @param values
	 * @param orderType
	 * @return
	 */
	public static <T> T selectOne(Class<T> clazz, String[] columns,
			String[] values, int orderType) {
		List<T> list = DAO.search(clazz, columns, values, null, orderType);
		return list == null ? null : list.get(0);
	}

	/**
	 * 查询单个记录，当给定条件只能查询单条记录时使用，按id排序且降序
	 * 
	 * @param <T>
	 * @param clazz
	 * @param columns
	 * @param values
	 * @return
	 */
	public static <T> T selectOne(Class<T> clazz, String[] columns,
			String[] values) {
		List<T> list = DAO.search(clazz, columns, values, null,
				OrderType.DESC_ORDER);
		return list == null ? null : list.get(0);
	}

	/**
	 * 查询某表的记录数
	 * 
	 * @param <T>
	 * @param clazz
	 * @return
	 */
	public static <T> int selectCount(Class<T> clazz) {
		return DAO.selectCount(clazz, null);
	}

	/**
	 * 查询某表的记录数(带WHERE条件)
	 * 
	 * @param <T>
	 * @param clazz
	 * @return
	 */
	public static <T> int selectCount(Class<T> clazz, String condition) {
		int result = -1;
		ORMConfigBean ormBean = ORMConfigBeanCache.get(clazz);
		String table = ormBean == null ? clazz.getSimpleName() : ormBean.getTable();
		
		String sql = String.format("SELECT COUNT(*) FROM %s ;", table);
		if (condition != null && condition.trim().length() > 0) {
			sql = String.format("SELECT COUNT(*) FROM %s WHERE %s;", table, condition);
		}

		StringBuilder sb = new StringBuilder("执行sql：");
		sb.append(sql).append("\n");
		Connection con = null;
		try {
			con = ConnectionManager.getInstance().getConnection();
			PreparedStatement pstmt = con.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next()) {
				result = rs.getInt(1);
				rs.close();
				pstmt.close();
			}
			if (con != null) {
				con.close();
			}

			// ----------log-------
			String debug = ((ConfigBean) SingleBeanCache
					.get(ConfigConstant.CONFIGBEAN_ID)).getOrm().getDebug();
			if ("true".equals(debug) || "1".equals(debug)) {
				System.out.println(sb.toString());
			}
			LogFactory.getORMLogger().write(sb.toString());
			LogUtil.getLogger().info(sb.toString());
			// ----------------------
		} catch (SQLException e) {
			LogUtil.getLogger().error(sb.toString(), e);
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 分页查询
	 * 
	 * @param <T>
	 * @param clazz
	 *            映射数据表的pojo类型
	 * @param currPage
	 *            需要查询的当前页码
	 * @param numPerPage
	 *            一页显示的记录条数
	 * @param orderColumn
	 *            对该字段进行排序
	 * @param orderType
	 *            升序还是降序
	 * @return 返回list集合
	 */
	public static <T> List<T> divPage(Class<T> clazz, int currPage,
			int numPerPage, String orderColumn, int orderType, String condition) {
		List<T> list = null;
		if (clazz != null) {
			try {
				String sql = SqlFactory.getSelectSql(clazz.newInstance())
						.divPage(currPage, numPerPage, orderColumn, orderType,
								condition);
				list = JdbcTemplate.getResultSet(clazz, sql);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	public static <T> List<T> divPage(Class<T> clazz, int currPage,
			int numPerPage, String orderColumn, int orderType) {
		List<T> list = null;
		if (clazz != null) {
			try {
				String sql = SqlFactory.getSelectSql(clazz.newInstance())
						.divPage(currPage, numPerPage, orderColumn, orderType,
								null);
				list = JdbcTemplate.getResultSet(clazz, sql);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	/**
	 * 分页查询，默认id排序
	 * 
	 * @param <T>
	 * @param clazz
	 * @param currPage
	 * @param numPerPage
	 * @param orderType
	 * @return
	 */
	public static <T> List<T> divPage(Class<T> clazz, int currPage,
			int numPerPage, int orderType) {
		return divPage(clazz, currPage, numPerPage, null, orderType);
	}

	public static <T> List<T> divPage(Class<T> clazz, int currPage,
			int numPerPage, int orderType, String condition) {
		return divPage(clazz, currPage, numPerPage, null, orderType, condition);
	}

	/**
	 * 分页查询 按id排序且降序
	 * 
	 * @param <T>
	 * @param clazz
	 * @param currPage
	 * @param numPerPage
	 * @return
	 */
	public static <T> List<T> divPage(Class<T> clazz, int currPage,
			int numPerPage) {
		return divPage(clazz, currPage, numPerPage, null, OrderType.DESC_ORDER);
	}

	public static <T> List<T> divPage(Class<T> clazz, int currPage,
			int numPerPage, String condition) {
		return divPage(clazz, currPage, numPerPage, null, OrderType.DESC_ORDER,
				condition);
	}

	public static <T> List<T> divPage(Class<T> clazz, String[] columns,
			String[] values, String orderColumn, int orderType, int currPage,
			int numPerPage) {
		List<T> list = null;
		if (clazz != null) {
			try {
				String sql = SqlFactory.getSelectSql(clazz.newInstance())
						.selectWhere(columns, values, orderColumn, orderType,
								currPage, numPerPage);
				list = JdbcTemplate.getResultSet(clazz, sql);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	public static <T> List<T> divPageNot(Class<T> clazz, String[] columns,
			String[] values, String orderColumn, int orderType, int currPage,
			int numPerPage) {
		List<T> list = null;
		if (clazz != null) {
			try {
				String sql = SqlFactory.getSelectSql(clazz.newInstance())
						.selectWhereNot(columns, values, orderColumn,
								orderType, currPage, numPerPage);
				list = JdbcTemplate.getResultSet(clazz, sql);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	/**
	 * 精确查询
	 * 
	 * @param <T>
	 * @param clazz
	 * @param columns
	 * @param values
	 * @return
	 */
	public static <T> List<T> search(Class<T> clazz, String[] columns,
			String[] values, String orderColumn, int orderType) {
		List<T> list = null;
		if (clazz != null) {
			try {
				String sql = SqlFactory.getSelectSql(clazz.newInstance())
						.selectWhere(columns, values, orderColumn, orderType);
				list = JdbcTemplate.getResultSet(clazz, sql);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	public static <T> List<T> searchNot(Class<T> clazz, String[] columns,
			String[] values, String orderColumn, int orderType) {
		List<T> list = null;
		if (clazz != null) {
			try {
				String sql = SqlFactory
						.getSelectSql(clazz.newInstance())
						.selectWhereNot(columns, values, orderColumn, orderType);
				list = JdbcTemplate.getResultSet(clazz, sql);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	public static <T> List<T> divPage(T t, String[] columns,
			String orderColumn, int orderType, int currPage, int numPerPage) {
		List<T> list = null;
		if (t != null) {
			Class<T> clazz = (Class<T>) t.getClass();
			try {
				String sql = SqlFactory.getSelectSql(t).selectWhere(columns,
						null, orderColumn, orderType, currPage, numPerPage);
				list = JdbcTemplate.getResultSet(clazz, sql);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	public static <T> List<T> divPageNot(T t, String[] columns,
			String orderColumn, int orderType, int currPage, int numPerPage) {
		List<T> list = null;
		if (t != null) {
			Class<T> clazz = (Class<T>) t.getClass();
			try {
				String sql = SqlFactory.getSelectSql(t).selectWhereNot(columns,
						null, orderColumn, orderType, currPage, numPerPage);
				list = JdbcTemplate.getResultSet(clazz, sql);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	public static <T> List<T> search(T t, String[] columns, String orderColumn,
			int orderType) {
		List<T> list = null;
		if (t != null) {
			Class<T> clazz = (Class<T>) t.getClass();
			try {
				String sql = SqlFactory.getSelectSql(t).selectWhere(columns,
						null, orderColumn, orderType);
				list = JdbcTemplate.getResultSet(clazz, sql);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	public static <T> List<T> searchNot(T t, String[] columns,
			String orderColumn, int orderType) {
		List<T> list = null;
		if (t != null) {
			Class<T> clazz = (Class<T>) t.getClass();
			try {
				String sql = SqlFactory.getSelectSql(t).selectWhereNot(columns,
						null, orderColumn, orderType);
				list = JdbcTemplate.getResultSet(clazz, sql);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	public static <T> List<T> search(Class<T> clazz, String[] columns,
			String[] values, int orderType) {
		return search(clazz, columns, values, null, orderType);
	}

	public static <T> List<T> searchNot(Class<T> clazz, String[] columns,
			String[] values, int orderType) {
		return searchNot(clazz, columns, values, null, orderType);
	}

	public static <T> List<T> divPage(Class<T> clazz, String[] columns,
			String[] values, int orderType, int currPage, int numPerPage) {
		return divPage(clazz, columns, values, null, orderType, currPage,
				numPerPage);
	}

	public static <T> List<T> divPageNot(Class<T> clazz, String[] columns,
			String[] values, int orderType, int currPage, int numPerPage) {
		return divPageNot(clazz, columns, values, null, orderType, currPage,
				numPerPage);
	}

	public static <T> List<T> search(T t, String[] columns, int orderType) {
		return search(t, columns, null, orderType);
	}

	public static <T> List<T> searchNot(T t, String[] columns, int orderType) {
		return searchNot(t, columns, null, orderType);
	}

	public static <T> List<T> divPage(T t, String[] columns, int orderType,
			int currPage, int numPerPage) {
		return divPage(t, columns, null, orderType, currPage, numPerPage);
	}

	public static <T> List<T> divPageNot(T t, String[] columns, int orderType,
			int currPage, int numPerPage) {
		return divPageNot(t, columns, null, orderType, currPage, numPerPage);
	}

	public static <T> List<T> search(Class<T> clazz, String[] columns,
			String[] values) {
		return search(clazz, columns, values, null, OrderType.DESC_ORDER);
	}

	public static <T> List<T> searchNot(Class<T> clazz, String[] columns,
			String[] values) {
		return searchNot(clazz, columns, values, null, OrderType.DESC_ORDER);
	}

	public static <T> List<T> divPage(Class<T> clazz, String[] columns,
			String[] values, int currPage, int numPerPage) {
		return divPage(clazz, columns, values, null, OrderType.DESC_ORDER,
				currPage, numPerPage);
	}

	public static <T> List<T> divPageNot(Class<T> clazz, String[] columns,
			String[] values, int currPage, int numPerPage) {
		return divPageNot(clazz, columns, values, null, OrderType.DESC_ORDER,
				currPage, numPerPage);
	}

	public static <T> List<T> search(T t, String[] columns) {
		return search(t, columns, OrderType.DESC_ORDER);
	}

	public static <T> List<T> searchNot(T t, String[] columns) {
		return searchNot(t, columns, OrderType.DESC_ORDER);
	}

	public static <T> List<T> divPage(T t, String[] columns, int currPage,
			int numPerPage) {
		return divPage(t, columns, OrderType.DESC_ORDER, currPage, numPerPage);
	}

	public static <T> List<T> divPageNot(T t, String[] columns, int currPage,
			int numPerPage) {
		return divPageNot(t, columns, OrderType.DESC_ORDER, currPage,
				numPerPage);
	}

	/**
	 * 模糊查询
	 * 
	 * @param <T>
	 * @param clazz
	 *            给定类型
	 * @param columns
	 *            给定字段
	 * @param values
	 *            给定字段的值
	 * @param likeType
	 *            模糊匹配类型，-1左匹配 0全匹配 1正匹配
	 * @param orderColumn
	 *            排序字段
	 * @param orderType
	 *            排序类型 -1降序 1升序
	 * @return 返回list集合
	 */
	public static <T> List<T> searchLike(Class<T> clazz, String[] columns,
			String[] values, int likeType, String orderColumn, int orderType) {
		List<T> list = null;
		if (clazz != null) {
			try {
				String sql = SqlFactory.getSelectSql(clazz.newInstance())
						.selectWhereLike(columns, values, likeType,
								orderColumn, orderType);
				list = JdbcTemplate.getResultSet(clazz, sql);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	public static <T> List<T> searchNotLike(Class<T> clazz, String[] columns,
			String[] values, int likeType, String orderColumn, int orderType) {
		List<T> list = null;
		if (clazz != null) {
			try {
				String sql = SqlFactory.getSelectSql(clazz.newInstance())
						.selectWhereNotLike(columns, values, likeType,
								orderColumn, orderType);
				list = JdbcTemplate.getResultSet(clazz, sql);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	public static <T> List<T> divPageLike(Class<T> clazz, String[] columns,
			String[] values, int likeType, String orderColumn, int orderType,
			int currPage, int numPerPage) {
		List<T> list = null;
		if (clazz != null) {
			try {
				String sql = SqlFactory.getSelectSql(clazz.newInstance())
						.selectWhereLike(columns, values, likeType,
								orderColumn, orderType, currPage, numPerPage);
				list = JdbcTemplate.getResultSet(clazz, sql);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	public static <T> List<T> divPageNotLike(Class<T> clazz, String[] columns,
			String[] values, int likeType, String orderColumn, int orderType,
			int currPage, int numPerPage) {
		List<T> list = null;
		if (clazz != null) {
			try {
				String sql = SqlFactory.getSelectSql(clazz.newInstance())
						.selectWhereNotLike(columns, values, likeType,
								orderColumn, orderType, currPage, numPerPage);
				list = JdbcTemplate.getResultSet(clazz, sql);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	public static <T> List<T> searchLike(T t, String[] columns, int likeType,
			String orderColumn, int orderType) {
		List<T> list = null;
		if (t != null) {
			Class<T> clazz = (Class<T>) t.getClass();
			try {
				String sql = SqlFactory.getSelectSql(t).selectWhereLike(
						columns, likeType, orderColumn, orderType);
				list = JdbcTemplate.getResultSet(clazz, sql);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	public static <T> List<T> searchNotLike(T t, String[] columns,
			int likeType, String orderColumn, int orderType) {
		List<T> list = null;
		if (t != null) {
			Class<T> clazz = (Class<T>) t.getClass();
			try {
				String sql = SqlFactory.getSelectSql(t).selectWhereNotLike(
						columns, likeType, orderColumn, orderType);
				list = JdbcTemplate.getResultSet(clazz, sql);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	public static <T> List<T> divPageLike(T t, String[] columns, int likeType,
			String orderColumn, int orderType, int currPage, int numPerPage) {
		List<T> list = null;
		if (t != null) {
			Class<T> clazz = (Class<T>) t.getClass();
			try {
				String sql = SqlFactory.getSelectSql(t).selectWhereLike(
						columns, likeType, orderColumn, orderType, currPage,
						numPerPage);
				list = JdbcTemplate.getResultSet(clazz, sql);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	public static <T> List<T> divPageNotLike(T t, String[] columns,
			int likeType, String orderColumn, int orderType, int currPage,
			int numPerPage) {
		List<T> list = null;
		if (t != null) {
			Class<T> clazz = (Class<T>) t.getClass();
			try {
				String sql = SqlFactory.getSelectSql(t).selectWhereNotLike(
						columns, likeType, orderColumn, orderType, currPage,
						numPerPage);
				list = JdbcTemplate.getResultSet(clazz, sql);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	/**
	 * 模糊查询 按id排序
	 * 
	 * @param <T>
	 * @param clazz
	 * @param columns
	 * @param values
	 * @param likeType
	 * @param orderType
	 * @return
	 */
	public static <T> List<T> searchLike(Class<T> clazz, String[] columns,
			String[] values, int likeType, int orderType) {
		return searchLike(clazz, columns, values, likeType, null, orderType);
	}

	public static <T> List<T> searchNotLike(Class<T> clazz, String[] columns,
			String[] values, int likeType, int orderType) {
		return searchNotLike(clazz, columns, values, likeType, null, orderType);
	}

	public static <T> List<T> divPageLike(Class<T> clazz, String[] columns,
			String[] values, int likeType, int orderType, int currPage,
			int numPerPage) {
		return divPageLike(clazz, columns, values, likeType, null, orderType,
				currPage, numPerPage);
	}

	public static <T> List<T> divPageNotLike(Class<T> clazz, String[] columns,
			String[] values, int likeType, int orderType, int currPage,
			int numPerPage) {
		return divPageNotLike(clazz, columns, values, likeType, null,
				orderType, currPage, numPerPage);
	}

	public static <T> List<T> searchLike(T t, String[] columns, int likeType,
			int orderType) {
		return searchLike(t, columns, likeType, null, orderType);
	}

	public static <T> List<T> searchNotLike(T t, String[] columns,
			int likeType, int orderType) {
		return searchNotLike(t, columns, likeType, null, orderType);
	}

	public static <T> List<T> divPageLike(T t, String[] columns, int likeType,
			int orderType, int currPage, int numPerPage) {
		return divPageLike(t, columns, likeType, null, orderType, currPage,
				numPerPage);
	}

	public static <T> List<T> divPageNotLike(T t, String[] columns,
			int likeType, int orderType, int currPage, int numPerPage) {
		return divPageNotLike(t, columns, likeType, null, orderType, currPage,
				numPerPage);
	}

	public static <T> List<T> searchLike(Class<T> clazz, String[] columns,
			String[] values, int likeType) {
		return searchLike(clazz, columns, values, likeType, null,
				OrderType.DESC_ORDER);
	}

	public static <T> List<T> searchNotLike(Class<T> clazz, String[] columns,
			String[] values, int likeType) {
		return searchNotLike(clazz, columns, values, likeType, null,
				OrderType.DESC_ORDER);
	}

	public static <T> List<T> divPageLike(Class<T> clazz, String[] columns,
			String[] values, int likeType, int currPage, int numPerPage) {
		return divPageLike(clazz, columns, values, likeType, null,
				OrderType.DESC_ORDER, currPage, numPerPage);
	}

	public static <T> List<T> divPageNotLike(Class<T> clazz, String[] columns,
			String[] values, int likeType, int currPage, int numPerPage) {
		return divPageNotLike(clazz, columns, values, likeType, null,
				OrderType.DESC_ORDER, currPage, numPerPage);
	}

	public static <T> List<T> searchLike(T t, String[] columns, int likeType) {
		return searchLike(t, columns, likeType, null, OrderType.DESC_ORDER);
	}

	public static <T> List<T> searchNotLike(T t, String[] columns, int likeType) {
		return searchNotLike(t, columns, likeType, null, OrderType.DESC_ORDER);
	}

	public static <T> List<T> searchLike(T t, String[] columns, int likeType,
			int currPage, int numPerPage) {
		return divPageLike(t, columns, likeType, null, OrderType.DESC_ORDER,
				currPage, numPerPage);
	}

	public static <T> List<T> searchNotLike(T t, String[] columns,
			int likeType, int currPage, int numPerPage) {
		return divPageNotLike(t, columns, likeType, null, OrderType.DESC_ORDER,
				currPage, numPerPage);
	}

	/**
	 * 查询下一条
	 * 
	 * @param <T>
	 * @param t
	 *            给定对象
	 * @return 返回对象
	 */
	public static <T> T nextOne(T t) {
		T result = null;
		if (t != null) {
			Class<T> clazz = (Class<T>) t.getClass();
			try {
				String sql = SqlFactory.getSelectSql(t).nextOne();
				List<T> list = JdbcTemplate.getResultSet(clazz, sql);
				if (list != null && !list.isEmpty()) {
					result = list.get(0);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 按给定字段和值查询下一条
	 * 
	 * @param <T>
	 * @param clazz
	 * @param column
	 * @param value
	 * @return
	 */
	public static <T> T nextOne(Class<T> clazz, String column, String value) {
		T result = null;
		if (clazz != null) {
			try {
				T t = clazz.newInstance();
				String sql = SqlFactory.getSelectSql(t).nextOne(column, value);
				List<T> list = JdbcTemplate.getResultSet(clazz, sql);
				if (list != null && !list.isEmpty()) {
					result = list.get(0);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 查询上一条
	 * 
	 * @param <T>
	 * @param t
	 *            给定对象
	 * @return 返回对象
	 */
	public static <T> T preOne(T t) {
		T result = null;
		if (t != null) {
			Class<T> clazz = (Class<T>) t.getClass();
			try {
				String sql = SqlFactory.getSelectSql(t).preOne();
				List<T> list = JdbcTemplate.getResultSet(clazz, sql);
				if (list != null && !list.isEmpty()) {
					result = list.get(0);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 按给定字段和值查询下一条
	 * 
	 * @param <T>
	 * @param clazz
	 * @param column
	 * @param value
	 * @return
	 */
	public static <T> T preOne(Class<T> clazz, String column, String value) {
		T result = null;
		if (clazz != null) {
			try {
				T t = clazz.newInstance();
				String sql = SqlFactory.getSelectSql(t).preOne(column, value);
				List<T> list = JdbcTemplate.getResultSet(clazz, sql);
				if (list != null && !list.isEmpty()) {
					result = list.get(0);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	public static <T> T selectOneWhere(Class<T> clazz, String condition) {
		T result = null;
		if (clazz != null) {
			try {
				T t = clazz.newInstance();
				String sql = SqlFactory.getSelectSql(t).select(condition);
				List<T> list = JdbcTemplate.getResultSet(clazz, sql);
				if (list != null && !list.isEmpty()) {
					result = list.get(0);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return result;
	}

	public static <T> List<T> selectListWhere(Class<T> clazz, String condition) {
		List<T> result = null;
		if (clazz != null) {
			try {
				T t = clazz.newInstance();
				String sql = SqlFactory.getSelectSql(t).select(condition);
				result = JdbcTemplate.getResultSet(clazz, sql);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return result;
	}

	public static <T> List<T> selectBySQL(Class<T> clazz, String... sqls) {
		List<T> result = null;
		if (clazz != null) {
			try {
				T t = clazz.newInstance();
				result = JdbcTemplate.getResultSet(clazz, sqls);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return result;
	}

	public static <T> List<T> selectBySQL(Class<T> clazz, String sql,
			Object... args) {
		List<T> result = null;
		if (clazz != null) {
			try {
				T t = clazz.newInstance();
				result = JdbcTemplate.getResultSetWithArgs(clazz, sql, args);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return result;
	}

	public static <T> boolean updateBySQL(Class<T> clazz, String... sqls) {
		boolean result = false;
		if (clazz != null) {
			try {
				result = JdbcTemplate.update(clazz, sqls);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return result;
	}

	public static <T> boolean updateBySQL(Class<T> clazz, String sql,
			Object... args) {
		boolean result = false;
		if (clazz != null) {
			try {
				result = JdbcTemplate.updateWhithArgs(clazz, sql, args);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return result;
	}

}
